HAL CEC initialization and configuration functions migration

The goal of this page is to show how to migrate the Init sequence using STM32CubeMX2:

  • Open STM32CubeMX2 CEC configuration panel.

  • Configure the SW layer HAL

  • Configure the main CEC features.

  • Configure the CEC advanced features.

  • Configure the CEC GPIO pins.

  • Configure the NVIC if the application requires the CEC to be used in IT mode.

  • Generate the code.

HAL CEC configuration panel

Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

Project configuration panel

HAL CEC main features configuration

Configure the I2C main features.

CEC main feature configuration

HAL CEC advanced features configuration

Configure the advanced CEC features.

CEC advanced feature configuration

GPIO configuration

Configure gpio pins.

CEC GPIO configuration

NVIC configuration

Configure the NVIC in case of an interrupt.

CEC NVIC configuration

Generated CEC initialization and de-initialization sequences

The CEC initialization sequence is generated in the mx_cec_init function in mx_cec.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

Related concept:

STM32CubeMX2 Generated : CEC initialization and de-initialization sequences

Topic

HAL1 code snippet

HAL2 code snippet

Handle declaration



Initialization and configuration sequence

static void MX_HDMI_CEC_Init(void)
{


  hcec.Instance = CEC;






  hcec.Init.OwnAddress = CEC_OWN_ADDRESS_0;


  hcec.Init.SignalFreeTime = CEC_DEFAULT_SFT;


  hcec.Init.SignalFreeTimeOption = CEC_SFT_START_ON_TXSOM;


  hcec.Init.Tolerance = CEC_STANDARD_TOLERANCE;


  hcec.Init.ListenMode = CEC_FULL_LISTENING_MODE;


  hcec.Init.BRERxStop = CEC_RX_STOP_ON_BRE;


  hcec.Init.BREErrorBitGen = CEC_BRE_ERRORBIT_GENERATION;


  hcec.Init.LBPEErrorBitGen = CEC_LBPE_ERRORBIT_GENERATION;


  hcec.Init.BroadcastMsgNoErrorBitGen = CEC_BROADCASTERROR_ERRORBIT_GENERATION;

  hcec.Init.RxBuffer = cec_receive_buffer;
  if (HAL_CEC_Init(&hcec) != HAL_OK)
  {
    Error_Handler();
  }
}

void HAL_CEC_MspInit(CEC_HandleTypeDef* hcec)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  if(hcec->Instance==CEC)
  {
  /** Initializes the peripherals clock
  */
    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_CEC;
    PeriphClkInitStruct.CecClockSelection = RCC_CECCLKSOURCE_LSI;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
    {
      Error_Handler();
    }

    /* Peripheral clock enable */
    __HAL_RCC_CEC_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**HDMI_CEC GPIO Configuration
    PA15 (JTDI)     ------> CEC
    */
    GPIO_InitStruct.Pin = GPIO_PIN_15;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF4_CEC;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* CEC interrupt Init */
    HAL_NVIC_SetPriority(CEC_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(CEC_IRQn);
  }
}
hal_cec_handle_t *mx_cec_init(void)
{
  hal_cec_config_t cec_config;

  if (HAL_CEC_Init(&hCEC, HAL_CEC) != HAL_OK)
  {
    return NULL;
  }

  HAL_RCC_CEC_EnableClock();

  cec_config.own_addr = HAL_CEC_OWN_ADDR_0;
  HAL_CEC_SetConfig(&hCEC, &cec_config);

  /* Set signal free time duration */
  HAL_CEC_SetSignalFreeTimeDuration(&hCEC,
                                    HAL_CEC_SIG_FREETIME_DURATION_AUTO);

  /* Set signal free time start */
  HAL_CEC_SetSignalFreeTimeStart(&hCEC,
                                 HAL_CEC_SIG_FREETIME_ON_MSG_START);

  /* Set RX tolerance */
  HAL_CEC_SetRxTolerance(&hCEC, HAL_CEC_RX_TOLERANCE_STANDARD);

  /* Set listen mode */
  HAL_CEC_SetListenMode(&hCEC, HAL_CEC_LISTEN_MODE_RECEIVE_ALL_MSG);

  /* Enable the reception stop behavior upon bit rising error */
  HAL_CEC_EnableRxStopUponBitRisingErr(&hCEC);

  /* Enable the bit rising error */
  HAL_CEC_EnableBitRisingErrGen(&hCEC);

  /* Enable the long bit period error generation */
  HAL_CEC_EnableLongBitPeriodErrGen(&hCEC);

  /* Enable error-bit generation in broadcast message */
  HAL_CEC_EnableBroadcastErrGen(&hCEC);







  /* CEC GPIO Configuration *************************************/

  HAL_RCC_GPIOA_EnableClock();

  hal_gpio_config_t  gpio_config;

  /**
    CEC GPIO Configuration

    [GPIO Pin] ------> [Signal Name]

      PA15    ------>   CEC
    **/
  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_OPENDRAIN;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_4;
  HAL_GPIO_Init(HAL_GPIOA, HAL_GPIO_PIN_15, &gpio_config);

  /* CEC NVIC Configuration **************************************/
  HAL_CORTEX_NVIC_SetPriority(CEC_IRQn,
                              HAL_CORTEX_NVIC_PREEMP_PRIORITY_0,
                              HAL_CORTEX_NVIC_SUB_PRIORITY_0);
  HAL_CORTEX_NVIC_EnableIRQ(CEC_IRQn);

  return &hCEC;
}

Deinitialization sequence

void HAL_CEC_MspDeInit(CEC_HandleTypeDef* hcec)
{
  if(hcec->Instance==CEC)
  {
    /* Peripheral clock disable */
    __HAL_RCC_CEC_CLK_DISABLE();

    /**HDMI_CEC GPIO Configuration
    PA15 (JTDI)     ------> CEC
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_15);

    /* CEC interrupt DeInit */
    HAL_NVIC_DisableIRQ(CEC_IRQn);
  }

}
void mx_cec_deinit(void)
{
  /* Disable the interruption for CEC */
  HAL_CORTEX_NVIC_DisableIRQ(CEC_IRQn);

  /* De-initialize the CEC */
  HAL_CEC_DeInit(&hCEC);

  HAL_RCC_CEC_DisableClock();
  /* CEC GPIO Configuration **************************************/
  /* De-initialize all GPIO pins associated with CEC */
  HAL_GPIO_DeInit(HAL_GPIOA, HAL_GPIO_PIN_15);
}

IRQ handle function